package Controllers;

import Connection.PhaseLoader;
import Connection.User;
import Connection.WebPhaseLoader;
import Entities.Phase;
import SwingComponents.IDButton;
import SwingComponents.SelectPhasePanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class SelectPhaseController implements ActionListener{

    private PrincipalController principalController;
    private int id;
    private PhaseLoader loadPhase;
    private SelectPhasePanel selectPhasePanel;
    private User user;
    
    public SelectPhaseController(PrincipalController principalController, int id, User user) {
        this.id = id;
        this.principalController = principalController;
        this.user = user;
    }

    public void execute(){
        loadPhase = new WebPhaseLoader(principalController.getApplication());
        initSelectPhasePanel();
    }
    
    private void initSelectPhasePanel() {
        selectPhasePanel = new SelectPhasePanel(this, loadPhase.loadImagePhaseList(id));
        selectPhasePanel.setVisible(true);
        principalController.getApplication().add(selectPhasePanel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Back")) {
            selectPhasePanel.setVisible(false);
            principalController.getApplication().remove(selectPhasePanel);
            principalController.getPrincipalPanel().setVisible(true);
        } else {
            IDButton boton = (IDButton) e.getSource();
            Integer number = boton.getId();
            WebPhaseLoader phaseLoader = new WebPhaseLoader(principalController.getApplication());
            Phase phase = phaseLoader.loadPhase(id, number);
            selectPhasePanel.setVisible(false);
            ArkanoidController arkanoidController = new ArkanoidController(principalController, phase, user);
            arkanoidController.execute();
        }
    }

    public Integer getId(){
        return id;
    }

    public PrincipalController getPrincipalController() {
        return principalController;
    }
}
